<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="工资福利大类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bigCategory">
              <j-dict-select-category :disabled="formDisabled" v-model="model.bigCategory" placeholder="请选择工资福利大类" parentCode="A01A01" :async="true"  @blur="modifyBigCategory"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="工资福利小类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="smallCategory">
              <j-dict-select-category :disabled="formDisabled || !model.bigCategory" v-model="model.smallCategory" placeholder="请选择工资福利小类" :parentCode="model.bigCategory" :async="true"  @blur="modifyCategory"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="发票类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isSpecial">
              <j-dict-select-tag :disabled="formDisabled" type="list" @input="isSpecial(1)" v-model="model.isSpecial" dictCode="cost_invoice_type" placeholder="请选择发票类型" />
            </a-form-model-item>
          </a-col>
            <a-col :span="12">
            <a-form-model-item label="报销金额(元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amount">
              <a-input-number :disabled="formDisabled" :max="99999999" :min="0" @change="inputAmount(1)" v-model="model.amount" placeholder="请输入报销金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="税费金额(元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taxation">
              <a-input-number :disabled="formDisabled || isSpecialDisabled"  @change="inputAmount(1)"  :max="model.amount" :min="0"  v-model="model.taxation" placeholder="请输入税费金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="未税金额(元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="untaxedAmount">
              <a-input-number :max="99999999" :min="0"  v-model="model.untaxedAmount" placeholder="请输入未税金额" style="width: 100%" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="税率(%)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taxRate">
              <a-input-number :disabled="formDisabled || isTaxRateDisabled" :max="99999999" :min="0" :precision="0" v-model="model.taxRate" placeholder="请输入税率" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-input :disabled="formDisabled" v-model="model.isMust" placeholder="是否必填" :hidden="true" ></a-input>
          <a-input :disabled="formDisabled" v-model="model.checkPosition" placeholder="审核岗位" :hidden="true"></a-input>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>
<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import SelectProjectTaskInput from '@/views/scy/components/SelectProjectTaskInput'
  import SelectProjectInput from '@/views/scy/components/SelectProjectInput'
  import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
  import JDictSelectCategory from '@/components/dict/JDictSelectCategory.vue'

  export default {
    name: 'ErpFinanceReimbursementSalaryFeeForm',
    components: {
      SelectProjectTaskInput,
      SelectProjectInput,
      SelectTeamInput,
      JDictSelectCategory
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      teamId: {
        type: String,
        default: null
      }
    },
    data () {
      return {
        contentFlag:false,
        isSpecialDisabled:false,
        isTaxRateDisabled:false,
        model:{
        },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
          taxRate: [
              { required: true, message: '请输入正确的税率!'},
            ],
          bigCategory:[{ required: true, message: '请输入工资福利大类!'}],
          smallCategory:[{ required: true, message: '请输入工资福利小类!'},],
          amount:[ { required: true,pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},],
          //isSpecial:[ { required: true, message: '请选择发票类型!'},]
        },
        url: {
          add: "/api/erp/finance/reimbursement/erpFinanceReimbursementDailyFeeInfo/add",
          edit: "/api/erp/finance/reimbursement/erpFinanceReimbursementDailyFeeInfo/edit",
          queryById: "/api/erp/finance/reimbursement/erpFinanceReimbursementDailyFeeInfo/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      modifyBigCategory(data){
        if(data){
          this.$set(this.model,"isMust",data[0].isMust);
          if(this.model.smallCategory){
            this.$set(this.model,"smallCategory",null);
            this.$set(this.model,"checkPosition",null);
          }
          if(this.model.isMust=='1'){
            this.$set(this.validatorRules,"isSpecial",[ { required: true, message: '请选择发票类型!'},]);
          }else{
            this.$set(this.validatorRules,"isSpecial",[]);
          }
        }
      },
      //修改大类，清空小类
      modifyCategory(data){
        if(data){
          this.$set(this.model,"checkPosition",data[0].checkPosition);
        }
      },
      //发票类型
      isSpecialInit(arg){
          // 0 普票 1专票 2其他
        if(this.model.isMust=='1'){
          this.$set(this.validatorRules,"isSpecial",[ { required: true, message: '请选择发票类型!'},]);
        }else{
          this.$set(this.validatorRules,"isSpecial",[]);
        }
        if(this.model.isSpecial == '0'){ // 普票：税费和税率默认为0，发起人不可编辑和修改（流程中也不可修改）
            // this.$set(this.model, 'taxation',0);
            // this.$set(this.model, 'taxRate',0);
            this.$delete(this.validatorRules, 'taxation');
            this.$delete(this.validatorRules, 'taxRate');
            this.$refs.form.clearValidate('taxation');
            this.$refs.form.clearValidate('taxRate');
            // this.$set(this.validatorRules, 'taxation', [{ required: false,pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'}]);
            this.isSpecialDisabled=true;
            this.isTaxRateDisabled=true;
        }else if (this.model.isSpecial == '1'){ // 专票：税费和税率为必填，发起人可编辑和修改 （税率计算公式：税费金额/未税金额取整，流程中都可修改）
          this.$refs.form.clearValidate('taxation');
          // this.$set(this.model, 'taxation',null);
          // this.$set(this.model, 'taxRate',null);
          this.$set(this.validatorRules, 'taxRate', [{ required: true, message: '请输入正确的税率!'}]);
          this.$set(this.validatorRules, 'taxation', [{ required: true,pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'}]);
          this.isSpecialDisabled=false;
          this.isTaxRateDisabled=false;
        } else { // 其他或者不填：税费金额必填默认为0，发起人可修改，税率默认为0，发起人不可编辑和修改（流程中可以修改税费金额，不可修改税率）
          // this.$set(this.model, 'taxation',0);
          // this.$set(this.model, 'taxRate',0);
          this.$delete(this.validatorRules, 'taxRate');
          this.$set(this.validatorRules, 'taxation', [{ required: true,pattern: /^(([0-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'}]);
          this.$refs.form.clearValidate('taxation');
          this.$refs.form.clearValidate('taxRate');
          this.isSpecialDisabled=false;
          this.isTaxRateDisabled=true;
        }
      },
      //发票类型
      isSpecial(arg){
        // 0 普票 1专票 2其他
        if(this.model.isMust=='1'){
          this.$set(this.validatorRules,"isSpecial",[ { required: true, message: '请选择发票类型!'},]);
        }else{
          this.$set(this.validatorRules,"isSpecial",[]);
        }
        if(this.model.isSpecial == 0){ // 普票：税费和税率默认为0，发起人不可编辑和修改（流程中也不可修改）
            this.$set(this.model, 'taxation',0);
            this.$set(this.model, 'taxRate',0);
            this.$delete(this.validatorRules, 'taxation');
            this.$delete(this.validatorRules, 'taxRate');
            this.$refs.form.clearValidate('taxation');
            this.$refs.form.clearValidate('taxRate');
            this.isSpecialDisabled=true;
            this.isTaxRateDisabled=true;
        }else if (this.model.isSpecial == 1){ // 专票：税费和税率为必填，发起人可编辑和修改 （税率计算公式：税费金额/未税金额取整，流程中都可修改）
          this.$refs.form.clearValidate('taxation');
          this.$set(this.model, 'taxation',null);
          this.$set(this.model, 'taxRate',null);
          this.$set(this.validatorRules, 'taxRate', [{ required: true, message: '请输入正确的税率!'}]);
          this.$set(this.validatorRules, 'taxation', [{ required: true,pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'}]);
          this.isSpecialDisabled=false;
          this.isTaxRateDisabled=false;
        } else { // 其他或者不填：税费金额必填默认为0，发起人可修改，税率默认为0，发起人不可编辑和修改（流程中可以修改税费金额，不可修改税率）
          this.$set(this.model, 'taxation',0);
          this.$set(this.model, 'taxRate',0);
          this.$delete(this.validatorRules, 'taxRate');
          this.$set(this.validatorRules, 'taxation', [{ required: true,pattern: /^(([0-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'}]);
          this.$refs.form.clearValidate('taxRate');
          this.isSpecialDisabled=false;
          this.isTaxRateDisabled=true;
        }
        this.inputAmount();
      },
      //输入报销金额
      inputAmount(){
        // 判断当有值的
        if(this.model.taxation === null || this.model.taxation === '' || this.model.taxation === undefined){
          this.$set(this.model,'untaxedAmount', null);
          return false
        }
        if(this.model.amount && (this.model.taxation || this.model.taxation==0)){
          let untaxedAmount=(this.model.amount-this.model.taxation).toFixed(2);
          if(untaxedAmount>0){
              // 未税金额 = 报销金额 - 税费金额 
              this.$set(this.model,'untaxedAmount',untaxedAmount);
              if (this.model.isSpecial == 1) {
                this.$set(this.model,'taxRate',(this.model.taxation/this.model.untaxedAmount*100).toFixed(0));
              } else {
                this.$set(this.model, 'taxRate',0);
              }
          }else{
            this.$set(this.model,'untaxedAmount',0);
              this.$set(this.model,'taxRate',0);
          }
        }
      },
      changeTask(data){
        if(data){
            this.model.taskName=data[0].taskDescription
        }
      },
      selectProject(data){
        if(data){
          this.model.performanceAttributionYear=data.choseObj[0].year;
          this.model.teamId=data.choseObj[0].teamName;
          if(this.model.taskCategory){
            this.$set(this.model,"taskName",null);
            this.$set(this.model,"taskCategory",'');
          }
        }
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs['form'].clearValidate();
        Object.assign(this.$data, this.$options.data())
        this.model = Object.assign({}, record);
        this.isSpecialInit();
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            //that.confirmLoading = true;
            if (!this.model.id) {
              this.model.id = parseInt(Math.random() * 1000000000000);
            }
            that.$emit('ok',this.model);
          }

        })
      },
    }
  
  }
</script>